package com.yitianyike.lockscreen.ces.handler;

import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yitianyike.lockscreen.ces.bo.AlmanacBO;
import com.yitianyike.lockscreen.ces.bo.VideoBO;
import com.yitianyike.lockscreen.ces.service.DataAccessFactory;

import net.sf.json.JSONObject;

public class CardDataHandler {
	private AlmanacBO almanacBO = (AlmanacBO) DataAccessFactory.dataHolder()
			.get("almanacBO");
	private VideoBO videoBO = (VideoBO) DataAccessFactory.dataHolder().get(
			"videoBO");

	private Map<String, String> parmMap;
	private ChannelHandlerContext ctx;
	private String content;

	public CardDataHandler(ChannelHandlerContext ctx, Map<String, String> map,
			String content) {
		this.parmMap = map;
		this.ctx = ctx;
		this.content = content;
	}

	void putAlmanac() {
		int[] setAlmanac = almanacBO.setAlmanac(parmMap);
		Map<String, Object> response = new HashMap<String, Object>();
		if (setAlmanac.length != 0) {
			response.put("mes", "success");
			response.put("code", 0);
		} else {
			response.put("mes", "error");
			response.put("code", 1);
		}

		FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1,
				io.netty.handler.codec.http.HttpResponseStatus.OK,
				Unpooled.wrappedBuffer(JSONObject.fromObject(response)
						.toString().getBytes()));
		ResponseGenerator.sendHttpResponse(ctx, res);

	}

	void putVideo() {
		Map<String, Object> response = new HashMap<String, Object>();
		String setVideo = videoBO.setVideo(parmMap);
		response.put("mes", setVideo);
		response.put("code", 1);
		FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1,
				io.netty.handler.codec.http.HttpResponseStatus.OK,
				Unpooled.wrappedBuffer(JSONObject.fromObject(response)
						.toString().getBytes()));
		ResponseGenerator.sendHttpResponse(ctx, res);

	}
}
